package com.example.shop_car_2208.fragment

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.shop_car_2208.entity.AddCarParam
import com.example.shop_car_2208.entity.AddCarResult
import com.example.shop_car_2208.entity.DeleteCarParam
import com.example.shop_car_2208.entity.SelectResult
import com.example.shop_car_2208.mvvm.BaseViewModel
import kotlinx.coroutines.launch

class CarViewModel:BaseViewModel() {
    val carSuccess = MutableLiveData<SelectResult>()
    val carFail = MutableLiveData<String>()
    fun select(){
        viewModelScope.launch {
            try {
                carSuccess.value = apiService.getCar()
            }catch (e:Exception){
                carFail.value = e.message
            }
        }
    }
    val addSuccess = MutableLiveData<AddCarResult>()
    val addFail = MutableLiveData<String>()
    fun AddCar(id:Int,count:Int){
        viewModelScope.launch {
            try {
                val param = AddCarParam(count,id)
                addSuccess.value = apiService.getAddCar(param)
            }catch (e:Exception){
                addFail.value = e.message
            }
        }
    }
    val  deleteSuccess = MutableLiveData<AddCarResult>()
    val deleteFail = MutableLiveData<String>()
    fun delete(ids:String){
        viewModelScope.launch {
            try {
                val param = DeleteCarParam(ids)
                deleteSuccess.value = apiService.getDeleteCar(param)
            }catch (e:Exception){
                deleteFail.value = e.message
            }
        }
    }
}